#include <functional>
#include <iostream>
int divide(int v) { return v / 2; }
struct Divide {
  int value;
  int half() { return value / 2; };
};
struct divide_obj {
  int value;
  int operator()(int value) { return value / 2; };
};
int main() {
  std::function<int(int)> fn1 = divide;                      //函数
  std::function<int(int)> fn2 = &divide;                     //函数指针
  std::function<int(int)> fn3 = divide_obj();                //函数对象
  std::function<int(int)> fn4 = [](int a) { return a / 2; }; // lambda表达式
  std::function<int(int)> fn5 = std::negate<int>(); //标准function对象
  std::cout << fn1(10) << "\n";                     // 5
  std::cout << fn2(12) << "\n";                     // 6
  std::cout << fn3(14) << "\n";                     // 7
  std::cout << fn4(16) << "\n";                     // 8
  std::cout << fn5(18) << "\n";                     // -18
  Divide d{20};
  std::function<int(Divide &)> value = &Divide::value; //获取数据成员
  std::function<int(Divide &)> fn = &Divide::half;     //获取成员方法
  std::cout << "value:" << value(d) << "\n";           // 20
  std::cout << "half:" << fn(d) << "\n";               // 10
}
